﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate;
using Pcme.Info.MvcMusicStore.Models;
using Pcme.Info.MvcMusicStore.ViewModels;
using Pcme.Info.MvcMusicStore.Helpers;
using Pcme.Info.MvcMusicStore.Filter;

namespace Pcme.Info.MvcMusicStore.Controllers
{
    [CompressFilter(Order = 1)]
    [Authorize(Roles = "Administrator")]
    public class StoreManagerController : Controller
    {
        ISession sessioin = NHibernateHelper.GetSession();

        //
        // GET: /StoreManager/

        public ActionResult Index()
        {
            var albums = sessioin.CreateCriteria<Album>().List<Album>();

            return View(albums);
        }

        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        // 
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            var viewModel = new StoreManagerViewModel
            {
                Album = new Album()
                {
                    Genre = new Genre(),
                    Artist = new Artist(),
                    AlbumArtUrl = "/Content/Images/placeholder.gif",
                },
                Genres = sessioin.CreateCriteria<Genre>().List<Genre>(),
                Artists = sessioin.CreateCriteria<Artist>().List<Artist>()
            };

            return View(viewModel);
        }

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {

                album.Genre = sessioin.Get<Genre>(album.GenreId);
                album.Artist = sessioin.Get<Artist>(album.ArtistId);
                //Save Album
                sessioin.Save(album);
                sessioin.Flush();

                return RedirectToAction("Index");
            }

            // Invalid – redisplay with errors
            var viewModel = new StoreManagerViewModel
            {
                Album = album,
                Genres = sessioin.CreateCriteria<Genre>().List<Genre>(),
                Artists = sessioin.CreateCriteria<Artist>().List<Artist>()
            };

            return View(viewModel);
        }

        //
        // GET: /StoreManager/Edit/5

        public ActionResult Edit(int id)
        {
            var viewModel = new StoreManagerViewModel
            {
                Album = sessioin.Get<Album>(id, LockMode.Read),
                Genres = sessioin.CreateCriteria<Genre>().List<Genre>(),
                Artists = sessioin.CreateCriteria<Artist>().List<Artist>()
            };

            return View(viewModel);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var album = sessioin.Get<Album>(id, LockMode.Upgrade);

            try
            {
                //Save Album

                UpdateModel(album, "Album");

                album.Genre = sessioin.Get<Genre>(album.GenreId);
                album.Artist = sessioin.Get<Artist>(album.ArtistId);

                sessioin.Update(album);
                sessioin.Flush();

                return RedirectToAction("Index");
            }
            catch
            {
                //Error occurred – so redisplay the form

                var viewModel = new StoreManagerViewModel
                {
                    Album = album,
                    Genres = sessioin.CreateCriteria<Genre>().List<Genre>(),
                    Artists = sessioin.CreateCriteria<Artist>().List<Artist>()
                };

                return View(viewModel);
            }
        }

        //
        // GET: /StoreManager/Delete/5

        public ActionResult Delete(int id)
        {
            var album = sessioin.Get<Album>(id);

            return View(album);
        }

        [HttpPost]
        public ActionResult Delete(int id, string confirmButton)
        {
            /*var album = storeDB.Albums
                .Include("OrderDetails").Include("Carts")
                .Single(a => a.AlbumId == id);*/

            var album = sessioin.Get<Album>(id);

            // For simplicity, we're allowing deleting of albums 
            // with existing orders We've set up OnDelete = Cascade 
            // on the Album->OrderDetails and Album->Carts relationships

            sessioin.Delete(album);
            sessioin.Flush();

            return View("Deleted");
        }
    }
}
